const tools = require("./tools");
const local = require("./forLocalItems");
class Option{
    constructor( localName , defValue , type , father ){
        father = father || "once";
        this._v = undefined;
        this._father = tools.isType(father ,"String") ? null : father;
        this._isLocal =  father==="local";
        this._def = defValue;
        this._type = type || "Number";
        this._localName = localName;

        this._init();
    }

    _init(){
        if( tools.isType(this._v , "Undefined") ){
            let flg = false;
            let fv = this._isLocal ? local.get( this._localName ) : tools.selectValue(this._def , 0);
            if( this._isLocal && fv !=="0" && !fv){
                fv =  tools.selectValue(this._def , 0);
                flg = true;
            }
            this._v = this._type=="Number" ?  parseInt(fv) : fv;
            this._v = this._type=="String" ?  String(fv) : fv;

            flg && local.set( this._localName , this._v );
        }
    }

    getRef(){
        let keys = Array.prototype.slice.call(arguments);
        let key = keys[0] , ref = this._v ;
        if(key){
            while(key && keys.length>0){
                ref = ref[key] ? ref[key] : undefined;

                keys = Array.prototype.slice.call(keys , 1);
                key = keys[0];
            }
        }else{
            ref = this;
        }

        return ref;
    }
    
    get( assign ){
        return assign ? this : this._v;
    }

    getLocalName(){

    }

    getGloab(){
        return window.$G || window.Gloab;
    }

    getKeyPath(){
        let path = this._localName;
        path = this._father && this._father.getLocalName ? (this._father.getLocalName() + "." + path) : path;
        return path;
    }

    add( v , v2){
        if(this._type == 'Number'){
            this._v += v ;
        }else if( tools.isType(this._v , "Array") ){
            this._v.push( v );
        }else if( tools.isType(this._v , "Object") && v ){
            this._v[v] += v2 ;
        }else{
            this._v += v;
        }
        this.getGloab()._runKeyHandlers( this.getKeyPath()+".set" , v );
        // this._isLocal && local.set( this._localName , this._v );
        //openLocal && this._father.set( this._localName , this._v );
        return this;
    }

    push( v ){
        return this.add(v);
    }

    set( v , index ){
        let last;
        if(typeof index!="undefined"){
            last = this._v[index];
            this._v[index] = v;
        }else{
            last = this._v;
            this._v = this._type=="Number" ? parseInt(v) : v;
        }
        this.getGloab()._runKeyHandlers( this.getKeyPath()+".set" , [last , v] );
        // this._isLocal && local.set( this._localName , this._v );
        return this;
    }

    save(){
        if( this._isLocal ){
            local.set( this._localName , this._v );
        }else if(this._father){
            this._father.save();
        }
        this.getGloab()._runKeyHandlers( this.getKeyPath()+".save" , this._v );
        return this;
    }

    clear(){
        console.log("clear:"+localName);
        this._v = this._type=="Number" ? 0 : this._def;
        this._isLocal && local.set( this._localName , this._v );
        this.getGloab()._runKeyHandlers( this.getKeyPath()+".clear" );
    }
}

module.exports = Option; 